package com.fhj.movie.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ABCView extends View {

	private String[] abcStr = { "热", "A", "B", "C", "D", "E", "F", "G", "H",
			"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
			"V", "W", "X", "Y", "Z" };

	public ABCView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public ABCView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public ABCView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
	}

	private Paint abcPaint = new Paint();
	private int select=0;

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO 
		super.onDraw(canvas);
		
//		if(selected)
//		{
//			canvas.drawColor(Color.BLUE);
//		}
		
		int height = getHeight();
		int width = getWidth();
		int everyHeight = height/abcStr.length;
		for(int i=0;i<abcStr.length;i++)
		{
			abcPaint.setColor(Color.BLACK);
			//abcPaint.setTypeface(Typeface.DEFAULT_BOLD);//粗体
			abcPaint.setAntiAlias(true);
			abcPaint.setTextSize(22);
//			if(i==select){
//				abcPaint.setColor(Color.BLUE);
//				abcPaint.setFakeBoldText(true);
//			}
			float xPos = width/2-abcPaint.measureText(abcStr[i]);
			float yPos = everyHeight*i+everyHeight;
			canvas.drawText(abcStr[i], xPos, yPos, abcPaint);
		}
		
		
	}
	private OnTouchingABCChangedListener onTouchingABCChangedListener;
	private boolean selected = false;
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		// TODO 
		final int action = event.getAction();
		final float y = event.getY();
		final int oldSelect = select;
		final OnTouchingABCChangedListener listener = onTouchingABCChangedListener;
		final int c = (int)(y/getHeight()*abcStr.length);
		
		switch(action){
		case MotionEvent.ACTION_DOWN:
			selected = true;
			if(oldSelect != c&& listener!=null)
			{
				if(c>0&&c<abcStr.length)
				{
					listener.onTouchingABCChanged(abcStr[c]);
//					select = c;
//					invalidate();
				}
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if(oldSelect !=c&&listener!=null)
			{
				listener.onTouchingABCChanged(abcStr[c]);
//				select = c;
//				invalidate();
			}
			break;
		case MotionEvent.ACTION_UP:
			selected = false;
//			select = -1;
//			invalidate();
			break;
			
		}
		return true;
	}
	public void setOnTouchingABCChangedListener(
			OnTouchingABCChangedListener onTouchingABCChangedListener) {
		this.onTouchingABCChangedListener = onTouchingABCChangedListener;
	}
	public interface OnTouchingABCChangedListener{
		void onTouchingABCChanged(String s);
		
	}
}
